/*
 * Parser.cpp
 *
 *  Created on: 29/09/2011
 *      Author: diego
 */

#include "Parser.h"

Parser::Parser(Stopwords* stopwords) {
	this->stopword = stopwords;
}

void Parser::setDocument(string path){
	this->document = new ifstream();
	this->document->open(path.c_str());

	if (!this->document->good()) {
		//throw FileBadException("Error al abrir el documento");
		cout << "Error al abrir el documento";
	}
}

string Parser::nextWord(){
	this->word.clear();
	this->charLeido = 1;
	(*this->document) >> noskipws >> this->charLeido;
	while (!this->eof() && !this->isSeparator(this->charLeido)) {
		this->charLeido = this->toLowercase(this->charLeido);

		if (!this->isInvalidChar(this->charLeido)){
			this->word += this->charLeido;
		}
		(*this->document) >> noskipws >> this->charLeido;
	}
/*
	if (this->stopword->isStopword(this->word)){
		return "";
	}else{
		return this->word;
	}
	*/
	return this->word;
}

bool Parser::eof(){
	return this->document->eof();
}

void Parser::close(){
	this->document->close();
	delete this->document;
	this->document = NULL;
}

bool Parser::isSeparator(char c){
	char vector[] = {' ', '\n', '\t', '\b', '-', '#', '&', '.', ';', ':', ',','_','~'};
	int i;
	for (i = 0; i < 13; i++){
		if (c == vector[i]){
			return true;
		}
	}

	return false;
}

bool Parser::isInvalidChar(char c){
	if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)){
		//es un caracter valido
		return false;
	}else{
		// es un caracter invalido
		return true;
	}

	/*
	char vector[] = {' ','\n', '\t', '\b','"','\'','.',';',',',':','(',')','[',']','{','}','#','-','/','+','!','?','&','*','`','_','=','~'};
	int i;
	for (i = 0; i < 28; i++){
		if (c == vector[i]){
			return true;
		}
	}
	return false;
	*/
}

char Parser::toLowercase(char c){
	if (c >= 65 && c <= 90){
		return c + 32;
	}
	return c;
}

Parser::~Parser() {
	if (this->document != NULL){
		this->document->close();
		delete this->document;
	}
}

